﻿using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.Odbc;
using Vit.DataAccess.Sql;
using Vit.DataAccess.Oracle;

namespace Vit.DataAccess
{
	/// <summary>
	/// Contains factory methods for creating Database objects.
	/// </summary>
	public static class DatabaseFactory
	{
        private static IDataInstrumentationProvider defaultDataInstrumentationProvider = new NullDataInstrumentationProvider();

        public static void SetCurrentDataInstrumentationProvider(IDataInstrumentationProvider dataInstrumentationProvider)
        {
            if (dataInstrumentationProvider != null)
                defaultDataInstrumentationProvider = dataInstrumentationProvider;
        }

        public static Database Create(string connectionStringName)
        {
            var connectionConfig = ConfigurationManager.ConnectionStrings[connectionStringName];
            return Create(connectionConfig.ConnectionString, connectionConfig.ProviderName);
        }

        public static Database Create(string connectionString, string providerName)
        {
            Database db = null;
            switch (providerName)
            {
                case "System.Data.SqlClient":
                    db = new SqlDatabase(connectionString, defaultDataInstrumentationProvider);
                    break;
                case "System.Data.OracleClient":
                    db = new OracleDatabase(connectionString, null, defaultDataInstrumentationProvider);
                    break;
                case "System.Data.Odbc":
                    db = new GenericDatabase(connectionString, OdbcFactory.Instance, defaultDataInstrumentationProvider);
                    break;
                case "System.Data.OleDb":
                default:
                    db = new GenericDatabase(connectionString, OleDbFactory.Instance, defaultDataInstrumentationProvider);
                    break;
            }
            return db;
        }
	}
}
